<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="72px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="线上/线下" prop="online">
        <el-select v-model="queryParams.online" placeholder="请选择线上/线下" clearable>
          <el-option v-for="dict in dict.type.object_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.default_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['play:service:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="serviceList" row-key="id" :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <!-- <el-table-column label="上级服务id" prop="pid" /> -->
      <el-table-column label="名称" align="center" prop="name">
        <template slot-scope="scope">
          <template v-if="scope.row.pid === 0">
            <el-tag>
              {{ scope.row.name }}
            </el-tag>
          </template>
          <template v-else>
            {{ scope.row.name }}
          </template>
        </template>
      </el-table-column>

      <el-table-column label="图片" align="center" prop="pic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.pic" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="线上/线下" align="center" prop="online">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.object_type" :value="scope.row.online" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.default_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="城市编码" align="center" prop="cityCode" />
      <el-table-column label="代理id" align="center" prop="agencyId" /> -->
      <el-table-column label="排序" align="center" prop="no" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['play:service:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)"
            v-hasPermi="['play:service:add']">新增</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['play:service:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改服务项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="上级服务" prop="pid">
          <treeselect v-model="form.pid" :options="serviceOptions" :normalizer="normalizer" placeholder="请选择上级服务id" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="排序" prop="no">
          <!-- <el-input v-model="form.no" placeholder="请输入序号" /> -->
          <el-input-number v-model="form.no" :min="1" label="请输入排序" :step="1" step-strictly></el-input-number>
        </el-form-item>
        <el-form-item label="图片" prop="pic">
          <image-upload v-model="form.pic" :limit="1" />
        </el-form-item>
        <el-form-item label="线上/线下" prop="online">
          <!-- <el-input v-model="form.online" placeholder="请输入线上/线下" /> -->
          <el-radio-group v-model="form.online">
            <el-radio v-for="dict in dict.type.object_type" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.default_status" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述">
          <editor v-model="form.description" :min-height="192" />
        </el-form-item>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddServiceItems">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteServiceItems">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="serviceItemsList" :row-class-name="rowServiceItemsIndex"
          @selection-change="handleServiceItemsSelectionChange" ref="serviceItems" align="center">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="时长类型" prop="serviceCycle" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.serviceCycle" placeholder="请选择">
                <el-option v-for="dict in dict.type.object_pay_time_type" :key="dict.value" :label="dict.label" :value="dict.value">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="num" width="220">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.num" :min="1" label="输入数量" controls-position="right" :step="1" step-strictly></el-input-number>
            </template>
          </el-table-column>
          <el-table-column label="价格" prop="price" width="220">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.price" :precision="2" :step="0.1" :min="1" controls-position="right"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="no" width="220">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.no" :min="1" label="输入序号" controls-position="right" :step="1" step-strictly></el-input-number>
            </template>
          </el-table-column>
        </el-table>

        <!-- <el-form-item label="城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入城市" />
        </el-form-item>
        <el-form-item label="城市编码" prop="cityCode">
          <el-input v-model="form.cityCode" placeholder="请输入城市编码" />
        </el-form-item>
        <el-form-item label="代理id" prop="agencyId">
          <el-input v-model="form.agencyId" placeholder="请输入代理id" />
        </el-form-item> -->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listService, getService, delService, addService, updateService } from "@/api/play/service";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Service",
  dicts: ['default_status', 'object_type','object_pay_time_type'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 服务项目表格数据
      serviceList: [],
      // 服务项目树选项
      serviceOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        name: null,
        online: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入服务项目名称", trigger: "blur" }
        ],
        no: [
          { required: true, message: "请输入项目排列序号", trigger: "blur" }
        ],
        online: [
          { required: true, message: "请选择线上/线下", trigger: "blur" }
        ],
        status: [
          { required: true, message: "请选择状态", trigger: "blur" }
        ],
      },
      //付费项目
      serviceItemsList:[],
      // 子表选中数据
      checkedServiceItems: [],

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 订单款式添加按钮操作 */
    handleAddServiceItems() {
      let obj = {};
      obj.serviceCycle = "";
      obj.num = "";
      obj.price = "";
      obj.no = "";
      this.serviceItemsList.push(obj);
    },
    /** 订单款式删除按钮操作 */
    handleDeleteServiceItems() {
      if (this.checkedServiceItems.length == 0) {
        this.$modal.msgError("请选择要删除的数据");
      } else {
        const serviceItemsList = this.serviceItemsList;
        const checkedServiceItems = this.checkedServiceItems;
        this.serviceItemsList = serviceItemsList.filter(function (item) {
          return checkedServiceItems.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleServiceItemsSelectionChange(selection) {
      this.checkedServiceItems = selection.map(item => item.index)
    },
    /** 订单款式序号 */
    rowServiceItemsIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 查询服务项目列表 */
    getList() {
      this.loading = true;
      listService(this.queryParams).then(response => {
        this.serviceList = this.handleTree(response.data, "id", "pid");
        this.loading = false;
      });
    },
    /** 转换服务项目数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    /** 查询服务项目下拉树结构 */
    getTreeselect() {
      listService().then(response => {
        this.serviceOptions = [];
        const data = { id: 0, name: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "pid");
        this.serviceOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pid: null,
        name: null,
        pic: null,
        online: null,
        status: null,
        description: null,
        city: null,
        cityCode: null,
        agencyId: null,
        no: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.serviceItemsList = [];
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.pid = row.id;
      } else {
        this.form.pid = 0;
      }
      this.open = true;
      this.title = "添加服务项目";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.pid = row.pid;
      }
      getService(row.id).then(response => {
        this.form = response.data;
        this.serviceItemsList = response.data.serviceItems;
        this.open = true;
        this.title = "修改服务项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.serviceItems = this.serviceItemsList;
          if (this.form.id != null) {
            updateService(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addService(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除服务项目编号为"' + row.id + '"的数据项？').then(function () {
        return delService(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    }
  }
};
</script>
